Public Class frmManageOccultBlood

#Region "Declare Variables"
    Private m_eScreenMode As eScreenMode
    Private m_OccultBlood As New PoliceHealthClass.OccultBlood
    Private m_ManageOccultBloodControl As New PoliceHealthClass.ManageReferrenceControl
    Private IsPostBack As Boolean = False

    Private cbbStoolExamResultHistory As ComboBox

    Delegate Sub delLoadDatagridviewOccultBlood(ByVal intSelectRowIndex As Integer)
#End Region

#Region "Enumuration"
    Private Enum eScreenMode As Integer
        Initial
        View
        Add
        Edit
    End Enum
#End Region

#Region "General Functions"
    Private Sub SetScreenMode(ByVal ScreenMode As eScreenMode)
        m_eScreenMode = ScreenMode
        Select Case ScreenMode
            Case eScreenMode.Initial
                PoliceHealthClass.Utilities.EnableControl(False, btnSave, btnCancel, btnDelete, btnEdit, txtOccultBloodNameTha, txtOccultBloodNameEng)
                PoliceHealthClass.Utilities.EnableControl(True, btnAdd)

                dgvOccultBlood.Enabled = True

                If Not Me.IsPostBack Then

                    ConnectionString.ShowLoadingForm()
                    Me.Invoke(New delLoadDatagridviewOccultBlood(AddressOf LoadDatagridviewOccultBlood), 0)
                    ConnectionString.TerminateLoadingForm()


                    'LoadDatagridviewOccultBlood()
                    Me.IsPostBack = True
                End If

            Case eScreenMode.View
                PoliceHealthClass.Utilities.EnableControl(False, btnSave, btnCancel, txtOccultBloodNameTha, txtOccultBloodNameEng)
                PoliceHealthClass.Utilities.EnableControl(True, btnEdit, btnDelete, btnAdd)

                dgvOccultBlood.Enabled = True
            Case eScreenMode.Add
                PoliceHealthClass.Utilities.EnableControl(True, btnSave, btnCancel, txtOccultBloodNameTha, txtOccultBloodNameEng)
                PoliceHealthClass.Utilities.EnableControl(False, btnEdit, btnDelete, btnAdd)

                ClearFormData()
                dgvOccultBlood.Enabled = False

            Case eScreenMode.Edit
                PoliceHealthClass.Utilities.EnableControl(True, btnSave, btnCancel, txtOccultBloodNameTha, txtOccultBloodNameEng)
                PoliceHealthClass.Utilities.EnableControl(False, btnEdit, btnDelete, btnAdd)

                dgvOccultBlood.Enabled = False
        End Select


    End Sub


    Private Function VerifyInputData() As Boolean
        Return m_ManageOccultBloodControl.verifyInsert()
    End Function

    Private Sub LoadDatagridviewOccultBlood(Optional ByVal intSelectRowIndex As Integer = 0)
        dgvOccultBlood.Enabled = True
        m_OccultBlood.LoadOccultBlood()

        Dim intRowCount As Integer = 0
        dgvOccultBlood.Rows.Clear()
        While m_OccultBlood.NextRecord
            dgvOccultBlood.Rows.Add()
            dgvOccultBlood.Rows(intRowCount).Cells("Order").Value = intRowCount + 1
            dgvOccultBlood.Rows(intRowCount).Cells("OccultBloodID").Value = m_OccultBlood.occult_blood_id
            dgvOccultBlood.Rows(intRowCount).Cells("OccultBloodNameThai").Value = m_OccultBlood.occult_blood_name_tha
            dgvOccultBlood.Rows(intRowCount).Cells("OccultBloodNameEng").Value = m_OccultBlood.occult_blood_name_eng


            dgvOccultBlood.Rows(intRowCount).Selected = False


            intRowCount += 1
        End While

        If intSelectRowIndex < dgvOccultBlood.Rows.Count Then
            dgvOccultBlood.Rows(intSelectRowIndex).Selected = True
            dgvOccultBlood.CurrentCell = dgvOccultBlood.Rows(intSelectRowIndex).Cells(0)
        End If

        If dgvOccultBlood.Rows.Count > 0 Then
            SetScreenMode(eScreenMode.View)
            LoadDataForm(dgvOccultBlood.Rows(intSelectRowIndex).Cells("OccultBloodID").Value)
        Else
            If Me.IsPostBack Then
                SetScreenMode(eScreenMode.Initial)
            End If
            ClearFormData()
        End If
    End Sub

    Private Sub LoadDataForm(ByVal OccultBloodID As String)
        txtOccultBloodID.Text = OccultBloodID

        'Load Form
        If dgvOccultBlood.SelectedRows.Count > 0 Then
            txtOccultBloodNameTha.Text = dgvOccultBlood.SelectedRows(0).Cells("OccultBloodNameThai").Value
            txtOccultBloodNameEng.Text = dgvOccultBlood.SelectedRows(0).Cells("OccultBloodNameEng").Value
        Else
            txtOccultBloodNameTha.Text = dgvOccultBlood.Rows(0).Cells("OccultBloodNameThai").Value
            txtOccultBloodNameEng.Text = dgvOccultBlood.Rows(0).Cells("OccultBloodNameEng").Value
        End If

    End Sub

    Private Sub ClearFormData()
        txtOccultBloodNameTha.Text = ""
        txtOccultBloodNameEng.Text = ""
        PoliceHealthClass.Utilities.EnableControl(False, btnEdit)
    End Sub






#End Region

#Region "Event Handling Functions"
    Private Sub frmPolice_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'CMU_KPI_PHASE_1DataSet.tbError_Logging' table. You can move, or remove it, as needed.
        SetScreenMode(eScreenMode.Initial)
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        If dgvOccultBlood.SelectedRows.Count > 0 Then
            LoadDataForm(dgvOccultBlood.Rows(0).Cells("OccultBloodID").Value)
            SetScreenMode(eScreenMode.View)
        Else
            SetScreenMode(eScreenMode.Initial)
        End If
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim SelectIndex As Integer = 0
        Dim blVerifyInputControl As Boolean = False


        If m_eScreenMode = eScreenMode.Edit OrElse m_eScreenMode = eScreenMode.Add Then
            If VerifyInputData() Then

                If dgvOccultBlood.SelectedRows.Count > 0 Then
                    SelectIndex = dgvOccultBlood.SelectedRows(0).Index
                End If


                m_OccultBlood.occult_blood_id = PoliceHealthClass.Utilities.ClearEmptyToMinValue(txtOccultBloodID.Text, TypeCode.Int32)
                m_OccultBlood.occult_blood_name_tha = txtOccultBloodNameTha.Text
                m_OccultBlood.occult_blood_name_eng = txtOccultBloodNameEng.Text



                If m_eScreenMode = eScreenMode.Edit Then
                    m_ManageOccultBloodControl.showInsertResult(m_OccultBlood.updateOccultBlood(m_OccultBlood))
                ElseIf m_eScreenMode = eScreenMode.Add Then
                    m_ManageOccultBloodControl.showInsertResult(m_OccultBlood.insertOccultBlood(m_OccultBlood))
                End If

                ConnectionString.ShowLoadingForm()
                Me.Invoke(New delLoadDatagridviewOccultBlood(AddressOf LoadDatagridviewOccultBlood), dgvOccultBlood.SelectedRows(0).Index)
                ConnectionString.TerminateLoadingForm()

                'LoadDatagridviewOccultBlood(dgvOccultBlood.SelectedRows(0).Index)

            End If
        End If
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        SetScreenMode(eScreenMode.Add)
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        SetScreenMode(eScreenMode.Edit)
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        If m_ManageOccultBloodControl.verifyDelete() Then
            m_OccultBlood.occult_blood_id = txtOccultBloodID.Text
            m_ManageOccultBloodControl.showDeleteResult(m_OccultBlood.deleteOccultBlood(m_OccultBlood))

            ConnectionString.ShowLoadingForm()
            Me.Invoke(New delLoadDatagridviewOccultBlood(AddressOf LoadDatagridviewOccultBlood), 0)
            ConnectionString.TerminateLoadingForm()

            'LoadDatagridviewOccultBlood()
        End If
    End Sub

    Private Sub dgvOccultBlood_RowEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvOccultBlood.RowEnter
        LoadDataForm(dgvOccultBlood.Rows(e.RowIndex).Cells("OccultBloodID").Value)
    End Sub
#End Region
End Class